﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;
using System.IO;

namespace ThietBiSo.Controllers
{
    public class quangcaoController : Controller
    {
        //
        // GET: /quangcao/

        private MQuangCao msp = new MQuangCao();
        private QuangCao s = new QuangCao();

        public ActionResult Index()
        {
            ViewData["quangcaos"] = msp.FindAll();
            return View();
        }

        //
        // GET: /quangcao/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /quangcao/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /quangcao/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                // TODO: Add insert logic here
                //UploadFile();
                s.MaQuangCao = msp.GetMax() + 1;
                s.HinhAnh = collection.Get("HinhAnh");
                s.Link = collection.Get("Link");
                s.HienThi = false;
                s.ViTri = int.Parse(collection.Get("ddlViTri"));
                s.Target = int.Parse(collection.Get("ddlTarget"));
                s.LoaiQuangCao = int.Parse(collection.Get("ddlLoai"));
                if (msp.GetMaxTT() == 0 || msp.GetTotalQuangCao() == 0)
                {
                    s.ThuTu = 0;
                }
                else
                    s.ThuTu = msp.GetMaxTT() + 1;
                msp.Save(s);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        private void UploadFile()
        {
            string fileName = string.Empty;
            string filePathSave = string.Empty;
            foreach (string inputTagName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[inputTagName];
                if (file.ContentLength > 0)
                {
                    fileName = Path.GetFileNameWithoutExtension(file.FileName) + Path.GetExtension(file.FileName);
                    //filePathSave = Path.GetFileNameWithoutExtension(file.FileName) + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(file.FileName);
                    string filePath = Path.Combine(HttpContext.Server.MapPath("~/Content/FileUploads/QuangCao"), fileName);
                    // Save file
                    file.SaveAs(filePath);
                }
            }
            if (fileName == "" || fileName == null)
            {
                s.HinhAnh = "noImage";
            }
            else
            {
                s.HinhAnh = fileName;
            }


        }
        private void ReUploadFile(string oldFileName)
        {
            foreach (string inputTagName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[inputTagName];
                if (file.ContentLength > 0)
                {
                    //có file mới thi delete và insert lai
                    if (!string.IsNullOrEmpty(oldFileName))
                    {
                        DeleteFileOnServer(oldFileName);
                    }
                    UploadFile();
                }
            }
        }
        private void DeleteFileOnServer(string tenFileDinhKemLuu)
        {
            try
            {
                FileInfo file = new FileInfo(HttpContext.Server.MapPath("~/Content/FileUploads/QuangCao") + "/" + tenFileDinhKemLuu);
                if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch
            {
            }
        }

        //
        // GET: /quangcao/Edit/5

        public ActionResult Edit(int id)
        {
            s = msp.GetByID(id);
            ViewData["quangcaos"] = s;
            return View();
        }

        //
        // POST: /quangcao/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
               
                s.MaQuangCao = id;

                if (collection.Get("HinhAnh") == null || collection.Get("HinhAnh") == "")
                {
                    s.HinhAnh = collection.Get("HiddenHinhAnh");
                }
                else
                {
                    s.HinhAnh = collection.Get("HinhAnh");
                }

                s.Link = collection.Get("Link");
                s.ThuTu = int.Parse(collection.Get("ThuTu"));
                s.ViTri = int.Parse(collection.Get("ddlViTri"));
                s.Target = int.Parse(collection.Get("ddlTarget"));
                s.LoaiQuangCao = int.Parse(collection.Get("ddlLoai"));
                msp.Update(s);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /quangcao/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /quangcao/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
